#ifndef STLMODEL_H
#define STLMODEL_H

#include <QObject>
#include <QtOpenGL/qgl.h>
#include <QVector>
//#include <vector>
//#include <string>
#include <fstream>
#include <iostream>

using namespace std;

//! structural class defining faces for 3D models
class Face : QObject
{
	Q_OBJECT

public:
	//! defines a face from a normal and three edge vectors
	Face(float *normal, float *v1, float *v2, float *v3)
	{
		for (int n=0; n < 3; ++n) {
			_normal[n] = normal[n];
			_v1[n] = v1[n]; _v2[n] = v2[n]; _v3[n] = v3[n];
			_centroid[n] = (v1[n] + v2[n] + v3[n])/3;
		}
	};

	float _normal[3];
	float _v1[3];
	float _v2[3];
	float _v3[3];
	float _centroid[3];
};

//! class definition standard STL binary/ASCII 3D model
class STLModel : public QObject
{
	Q_OBJECT

public:
	//! provide the full model filename and units (inches or millimeters)
	STLModel(const QString& filename);
	//! copy constructor
	STLModel(STLModel& model);
	//! destructor
	~STLModel();

	QVector<Face*> _faces;
	
public slots:
	//! returns the pointer to the Face for manipulation or display
	Face* face(int index) {return _faces[index];}
	//! returns number of faces making up the model
	unsigned numFaces() {return (unsigned)_faces.size();}
	//! returns precomputed GL displaylists for the model, call inside GL draw code to render the model
	GLuint draw() {return _displayLists;}

private:
	void loadFromFile(const std::string& filename);
	void loadFromBinaryFile(const std::string& filename);
	void loadFromTextFile(const std::string& filename);

	GLuint _displayLists;
	
};

#endif // STLMODEL_H
